package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.pojo.*;

import com.example.demo.nuoqi.service.ResumemanagementService;
import com.example.demo.nuoqi.utils.JsonUtils;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "ResumemanagementController")
@RestController
@RequestMapping("/resumemanagement")
@CrossOrigin(value = "*")
public class ResumemanagementController {

    @Autowired
    private ResumemanagementService resumemanagementService;

    /***
     * Resumemanagement分页条件搜索实现
     * @param resumemanagement
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Resumemanagement条件分页查询",notes = "分页条件查询Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Resumemanagement对象",value = "传入JSON数据",required = false) Resumemanagement resumemanagement, @PathVariable int page, @PathVariable int size){
        //调用ResumemanagementService实现分页条件查询Resumemanagement
        PageInfo<Resumemanagement> pageInfo = resumemanagementService.findPage(resumemanagement, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Resumemanagement分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Resumemanagement分页查询",notes = "分页查询Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用ResumemanagementService实现分页查询Resumemanagement
        PageInfo<Resumemanagement> pageInfo = resumemanagementService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param resumemanagement
     * @return
     */
    @ApiOperation(value = "Resumemanagement条件查询",notes = "条件查询Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @PostMapping(value = "/search" )
    public Result<List<Resumemanagement>> findList(@RequestBody(required = false) @ApiParam(name = "Resumemanagement对象",value = "传入JSON数据",required = false) Resumemanagement resumemanagement){
        //调用ResumemanagementService实现条件查询Resumemanagement
        List<Resumemanagement> list = resumemanagementService.findList(resumemanagement);
        return new Result<List<Resumemanagement>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Resumemanagement根据ID删除",notes = "根据ID删除Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用ResumemanagementService实现根据主键删除
        resumemanagementService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Resumemanagement数据
     * @param resumemanagement
     * @param id
     * @return
     */
    @ApiOperation(value = "Resumemanagement根据ID修改",notes = "根据ID修改Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Resumemanagement对象",value = "传入JSON数据",required = false) Resumemanagement resumemanagement, @PathVariable Integer id){
        //设置主键值
        resumemanagement.setId(id);
        //调用ResumemanagementService实现修改Resumemanagement
        resumemanagementService.update(resumemanagement);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Resumemanagement数据
     * @param resumemanagement
     * @return
     */
    @ApiOperation(value = "Resumemanagement添加",notes = "添加Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Resumemanagement对象",value = "传入JSON数据",required = true) Resumemanagement resumemanagement , HttpSession session){
        //调用ResumemanagementService实现添加Resumemanagement
        User user = UserUtils.getUser(session);

        resumemanagement.setWorkExperience(JsonUtils.objectToJson(resumemanagement.getWorkExperiences()));
        resumemanagement.setEducationBackground(JsonUtils.objectToJson(resumemanagement.getEductionLists()));
        resumemanagement.setItemExperience(JsonUtils.objectToJson(resumemanagement.getProjectExperiences()));
        resumemanagement.setCreateName(user.getName());
        resumemanagement.setCreateId(user.getId());
        resumemanagement.setCreateTime(new Date());
        resumemanagement.setStatus("0");
        resumemanagementService.add(resumemanagement);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Resumemanagement数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Resumemanagement根据ID查询",notes = "根据ID查询Resumemanagement方法详情",tags = {"ResumemanagementController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Resumemanagement> findById(@PathVariable Integer id){
        //调用ResumemanagementService实现根据主键查询Resumemanagement
        Resumemanagement resumemanagement = resumemanagementService.findById(id);
        resumemanagement.setWorkExperiences(JsonUtils.jsonToList(resumemanagement.getWorkExperience(), workExperience.class));
        resumemanagement.setProjectExperiences(JsonUtils.jsonToList(resumemanagement.getItemExperience(), projectExperience.class));
        resumemanagement.setEductionLists(JsonUtils.jsonToList(resumemanagement.getEducationBackground(), eductionList.class));


        return new Result<Resumemanagement>(true,StatusCode.OK,"查询成功",resumemanagement);
    }

    /***
     * 查询Resumemanagement全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Resumemanagement",notes = "查询所Resumemanagement有方法详情",tags = {"ResumemanagementController"})
    @GetMapping
    public Result<List<Resumemanagement>> findAll(){
        //调用ResumemanagementService实现查询所有Resumemanagement
        List<Resumemanagement> list = resumemanagementService.findAll();
        List<Resumemanagement> temp = new ArrayList<>();

        for (Resumemanagement resumemanagement : list) {
            resumemanagement.setWorkExperiences(JsonUtils.jsonToList(resumemanagement.getWorkExperience(), workExperience.class));
            resumemanagement.setProjectExperiences(JsonUtils.jsonToList(resumemanagement.getItemExperience(), projectExperience.class));
            resumemanagement.setEductionLists(JsonUtils.jsonToList(resumemanagement.getEducationBackground(), eductionList.class));
            temp.add(resumemanagement);
        }

        return new Result<List<Resumemanagement>>(true, StatusCode.OK,"查询成功",temp) ;
    }



    /***
     * 根据当前登录用户简历
     * @return
     */
    @ApiOperation(value = "根据当前登录用户简历",notes = "根据当前登录用户简历",tags = {"ResumemanagementController"})
    @GetMapping("/findCurrentUserResume")
    public Result<List<Resumemanagement>> findCurrentUserResume(HttpSession session){
        User user = UserUtils.getUser(session);

        List<Resumemanagement> list = resumemanagementService.findCurrentUserResume(user.getId());
        List<Resumemanagement> temp = new ArrayList<>();

        for (Resumemanagement resumemanagement : list) {
            resumemanagement.setWorkExperiences(JsonUtils.jsonToList(resumemanagement.getWorkExperience(), workExperience.class));
            resumemanagement.setProjectExperiences(JsonUtils.jsonToList(resumemanagement.getItemExperience(), projectExperience.class));
            resumemanagement.setEductionLists(JsonUtils.jsonToList(resumemanagement.getEducationBackground(), eductionList.class));
            temp.add(resumemanagement);
        }

        return new Result<List<Resumemanagement>>(true, StatusCode.OK,"查询成功",temp) ;
    }



    @ApiOperation(value = "设置简历状态修改",notes = "设置简历状态修改",tags = {"ResumemanagementController"})
    @GetMapping("/setResumeResult/{resumeId}/{result}")
    public Result setResumeResult(@PathVariable("resumeId") Integer id , @PathVariable("result") String result) {
        Resumemanagement temp = resumemanagementService.findById(id);
        temp.setStatus(result);
        resumemanagementService.update(temp);
        return new Result(true, StatusCode.OK,"设置简历状态修改成功",temp) ;
    }
}
